package com.lanou.tipsy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseActivity;

import java.text.SimpleDateFormat;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by dllo on 15/11/5.
 *
 *      引导页轮播图
 *      fragment
 *      com.lanou.tipsy.fragment.guide
 *
 */
public class WelcomeActivity extends BaseActivity {
    public static final String TAG = "WelcomeActivity";
    private final int SPLASH_DISPLAY_LENGHT = 3000;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private LinearLayout start;//启动页首页ID
    private String date, dateJudge;//用于获取和对比系统时间
    private SimpleDateFormat sDateFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        preferences = getSharedPreferences("test", Context.MODE_PRIVATE);
        AlphaAnimation animation = new AlphaAnimation(1.0f, 1.0f);
        animation.setDuration(1500);
        start = (LinearLayout) findViewById(R.id.start);
        start.startAnimation(animation);
        //判断是否是预设日期的方法
        aprilFoolDay();

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (date.equals(dateJudge)) {
                            Intent intent = new Intent(WelcomeActivity.this, EggsActivity.class);
                            startActivity(intent);
                            finish();
                        }
                        if (preferences.getBoolean("firstStart", true)) {
                            editor = preferences.edit();
                            // 将登陆标识设置为false，下次登录时不再显示引导界面
                            editor.putBoolean("firstStart", false);
                            editor.commit();
                            Intent intent = new Intent(WelcomeActivity.this, AndyViewPagerActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                }, SPLASH_DISPLAY_LENGHT);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void aprilFoolDay(){
        //获取当前系统日期,
        dateJudge = "20151112";
        sDateFormat = new SimpleDateFormat("yyyyMMdd");
        date = sDateFormat.format(new java.util.Date());
        Log.i("test112",date);
        //如果是等于预设日期,则启动碎屏页面
        if (date.equals(dateJudge)){
            Intent intent = new Intent(WelcomeActivity.this, EggsActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

}
